package com.taomu.app.zongchouhuli.moudle.mine.contract.impl;

import com.alibaba.fastjson.JSONObject;
import com.taomu.app.zongchouhuli.entity.BaseChangeResponse;
import com.taomu.app.zongchouhuli.entity.BaseResponse;
import com.taomu.app.zongchouhuli.http.api.HttpObserver;
import com.taomu.app.zongchouhuli.http.api.Request;
import com.taomu.app.zongchouhuli.http.res.ResArticleEntity;
import com.taomu.app.zongchouhuli.http.res.ResLevenlEntity;
import com.taomu.app.zongchouhuli.http.utls.RxSchedulerHelper;
import com.taomu.app.zongchouhuli.moudle.mine.contract.UserMemberContract;
import com.taomu.app.zongchouhuli.utils.ParseUtil;

import io.reactivex.Observable;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Function;

public class UserMemberPresenter implements UserMemberContract.Presenter {

    private UserMemberContract.View mContractView;

    private CompositeDisposable disposable;

    public UserMemberPresenter(UserMemberContract.View mContractView){
        this.mContractView = mContractView;
        this.disposable = new CompositeDisposable();
    }

    @Override
    public void getLevenl() {
        Request.getIntence().getLevenl()
                .flatMap(new Function<String, Observable<BaseChangeResponse<ResLevenlEntity>>>() {
                    @Override
                    public  Observable<BaseChangeResponse<ResLevenlEntity>> apply(String json) throws Exception {
                        BaseResponse<JSONObject> baseResponse = ParseUtil.parseObject(json,BaseResponse.class);
                        BaseChangeResponse<ResLevenlEntity> data = new BaseChangeResponse<>();
                        data.setData(ParseUtil.parseObject(baseResponse.getData().toString(),ResLevenlEntity.class));
                        data.setCount(baseResponse.getCount());
                        return Observable.just(data);
                    }
                })
                .compose(RxSchedulerHelper.<BaseChangeResponse<ResLevenlEntity>>transMain())
                .subscribe(new HttpObserver<BaseChangeResponse<ResLevenlEntity>>(mContractView,disposable,true) {
                    @Override
                    public void onNext(BaseChangeResponse<ResLevenlEntity> baseResponse) {
                        if(mContractView != null){
                            mContractView.setLevenl(baseResponse.getData());
                        }
                    }
                });
    }

    @Override
    public void getArticleInfo() {
        Request.getIntence().getArticleInfo(String.valueOf(1))
                .compose(RxSchedulerHelper.<ResArticleEntity>transMain())
                .subscribe(new HttpObserver<ResArticleEntity>(mContractView,disposable,true) {
                    @Override
                    public void onNext(ResArticleEntity baseResponse) {
                        if(mContractView != null){
                            mContractView.setArticleInfo(baseResponse.getData().getArt_content_html());
                        }
                    }
                });

    }

    @Override
    public void onStart() {

    }

    @Override
    public void onDetch() {

    }
}
